package com.hntxk.cqzd.consultation.action;


import com.alibaba.fastjson.JSON;
import com.hntxk.cqzd.common.action.CqzdBaseController;
import com.hntxk.cqzd.consultation.entity.TblDibInfo;
import com.hntxk.cqzd.consultation.model.DibAndDiacModel;
import com.hntxk.cqzd.consultation.model.DibAndEmpModel;
import com.hntxk.cqzd.consultation.service.DibInfoService;
import com.hntxk.cqzd.consultation.service.impl.DibInfoServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("cqzd/do/dibinfo")
@Slf4j
public class DibInfoController extends CqzdBaseController
{


    /** 查询页面的path */

    private final static String LIST_JSP_PATH = "cqzd/consultation/dibInfo_list";

    private final DibInfoService dibInfoService;

    @Autowired
    public DibInfoController(@Qualifier("dibInfoManageService") DibInfoService dibInfoService) {
        super(dibInfoService, LogManager.getLogger(DibInfoController.class));
        this.dibInfoService = dibInfoService;
    }


    /**
     *
     * @return 患者会诊信息主页
     */
    @ApiIgnore
    @GetMapping(value = "/index")
    public String index() {


        return LIST_JSP_PATH;
    }

    /**
     * 查询会诊信息
     * @param model
     * @return
     */
    @ApiOperation("查询会诊信息")
    @RequestMapping(value = "/queryList",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String queryDibInfo(@ModelAttribute DibAndDiacModel model) {

        try {
            model.setPagination(request);
            return   toJson(model, dibInfoService.query2Json(DibInfoServiceImpl.DIBINFO_MAPPER_NAMESPACE + "query", model, true));
        } catch (Exception e) {

            return getError(e);
        }
    }

    /**
     * 查询会诊信息
     * @param
     * @return
     */
    @ApiOperation("根据右侧输入条件查询会诊信息")
    @RequestMapping(value = "/queryListByKey",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String queryDibInfoByKey(@RequestParam(required = false) String key) {
        DibAndDiacModel model=new DibAndDiacModel();
        if(key!=null&&key!=""){
            model.setPatientName(key);
        }
        try {
            return   toJson(model, dibInfoService.query2Json(DibInfoServiceImpl.DIBINFO_MAPPER_NAMESPACE + "query", model, true));
        } catch (Exception e) {
            return getError(e);
        }
    }


    /**
     * 获取会诊信息
     *
     * @return
     */
    @ApiOperation(value="获取会诊信息",notes = "产前诊断系统")
    @ApiImplicitParam(name = "id", value = "会诊id",example = "1",required = true, dataTypeClass = Long.class)
    @GetMapping(value = "/get/{id}", produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String get(@PathVariable("id") Long id){
        return toJson(dibInfoService.getById(id));
    }

    /**
     * 获取诊断记录的所有会诊信息
     *
     * @return
     */
    @ApiOperation(value="获取诊断记录的所有会诊信息",notes = "产前诊断系统")
    @ApiImplicitParam(name = "id", value = "诊断记录id",example = "1",required = true, dataTypeClass = Long.class)
    @GetMapping(value = "/getDibInfoById/{id}", produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getByDiacrisis(@PathVariable("id") Long id){
        TblDibInfo model = new TblDibInfo();
        model.setDiacrisisId(id);
        List<DibAndEmpModel> lists = dibInfoService.getByFkId(id);
        return toJson(lists);
    }




    //修改
    @PostMapping(value = "/updateDibInfo",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String updateDibInfoAndFile(@RequestParam("file") MultipartFile[] files,@RequestParam("json") String json) throws Exception{

        DibAndEmpModel dibAndEmpModel= JSON.parseObject(json,DibAndEmpModel.class);
        dibInfoService.update(dibAndEmpModel,files);
        return responseOk();
    }

    //新增
    @PostMapping(value = "/addDibInfo",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String addDibInfoAndFile(@RequestParam("file") MultipartFile[] files,@RequestParam("json") String json) throws Exception{

        DibAndEmpModel dibAndEmpModel= JSON.parseObject(json,DibAndEmpModel.class);
        dibInfoService.insert(dibAndEmpModel,files);
        return responseOk();
    }



    //查询某条诊断信息下的所有数据
    @RequestMapping(value = "/getDibInfoById/{id}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getDibInfo(@PathVariable Long id){
        return  toJson(dibInfoService.getDibInfo(id));
    }

    /**
     * 导出pdf
     * @param json 会诊页面对象DibAndEmpModel
     * @return
     */
    @RequestMapping(value = "getPdf",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public ResponseEntity<byte[]> getPdf(@RequestParam("json") String json) {
        DibAndEmpModel dibAndEmpModel= JSON.parseObject(json,DibAndEmpModel.class);
        ResponseEntity<byte[]> responseEntity = dibInfoService.exportPdf(dibAndEmpModel);
        byte[] bytes = responseEntity.getBody();
        UUID uuid= UUID.randomUUID();
        String fileName = uuid+ ".pdf";
        return getResponseEntity(bytes, fileName,null);
        //return responseEntity;
    }
}
